package edu.uta.cfl.GUI;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

public class ValidateInputFile {

	private Boolean isTxtFile = false;

	private Boolean isIPMFile = false;

	private Boolean ipmFile_info = false;

	private Boolean projectName = false;

	private Boolean validFile = false;
	
	private Boolean sourceFile = false;
	
	private Boolean scInfo = false;
	
	private Boolean rtsInfo = false;

	private BufferedReader br;

	private String str1;

	private String testData;

	private String ipmfile_Location;

	private String project_Name;
	
	private String rts_Location;
	
	private String sc_Location;
	
	private String sourceCodeLocation;

	public Boolean ValidFile(File file)

	{

		if (file.getName().toLowerCase().endsWith(".txt"))

		{

			// Check is it Text File?

			isTxtFile = true;

		}

		// check - a valid IPM File?

		try

		{

			br = new BufferedReader(new FileReader(file));

			while (((str1 = br.readLine()) != null))

			{

				if (str1.startsWith("# BEN"))

				{

					isIPMFile = true;

				}

				if (str1.startsWith("Project"))

				{

					Pattern identifyPattern = Pattern.compile(":");

					String[] tokens = identifyPattern.split(str1);

					project_Name = tokens[1].trim(); // Project Name should not
														// contain space in
														// betvalidFileween

					projectName = true;

				}

				if (str1.startsWith("IPM"))

				{

					Pattern identifyPattern = Pattern.compile(":");

					System.out.println("Test line # 65" + str1.toString());

					String[] tokens = identifyPattern.split(str1);

					System.out.println("Test line#65:" + tokens[1].toString());

					setIpmfile_Location(tokens[1].trim());

					ipmFile_info = true;
					

				} // end of IF loop
				
				if (str1.startsWith("Recommended"))

				{

					Pattern identifyPattern = Pattern.compile(":");

					String[] tokens = identifyPattern.split(str1);
					
					setRts_Location(tokens[1].trim());
					
					rtsInfo = true;
					
				} 
				
				if (str1.startsWith("Suspicious"))

				{

					Pattern identifyPattern = Pattern.compile(":");

					
					String[] tokens = identifyPattern.split(str1);

					
					setSc_Location(tokens[1].trim());
					
					scInfo = true;
				} 
				
				if (str1.startsWith("Source"))

				{

					Pattern identifyPattern = Pattern.compile(":");

					String[] tokens = identifyPattern.split(str1);
					
					setSourceCodeLocation(tokens[1].trim());
					
					sourceFile = true;
					
				} 
				
				
				/*
				if (str1.startsWith("Recommended"))
				{
					while(((str1 = br.readLine()) != null))
						{
							//System.out.println("Testing:  line # 109" + str1.toString());
							str1 = str1.replace(" ", "");
							if(str1.startsWith("File"))
							{
								Pattern identifyPattern = Pattern.compile(":");

								System.out.println("Test line # 114" + str1.toString());

								String[] tokens = identifyPattern.split(str1);

								System.out.println("Test line#114:" + tokens[1].toString());
								
								setRts_Location(tokens[1].trim());
								break;

							}
							
						}
				}
				
				if (str1.startsWith("Suspicious"))
				{
					while(((str1 = br.readLine()) != null))
						{
						str1 = str1.replace(" ", "");	
						if(str1.startsWith("File"))
							{
								str1 = str1.replace(" ", "");
								
								Pattern identifyPattern = Pattern.compile(":");

								System.out.println("Test line # 144" + str1.toString());

								String[] tokens = identifyPattern.split(str1);

								System.out.println("Test line#144:" + tokens[1].toString());
								
								setSc_Location(tokens[1].trim());
								break;

							}
						}
				}
	*/ //commenting out existing logic to read
			} // end of while loop

			if ((isIPMFile == true) && (ipmFile_info == true)
					&& (projectName == true) && (sourceFile == true)&& (scInfo == true) && (rtsInfo==true))

			{

				// Valid input file

				validFile = true;

			}

		} // end of try block

		catch (FileNotFoundException e)

		{

			e.printStackTrace();

		} catch (IOException e1) {

			// TODO Auto-generated catch block

			e1.printStackTrace();

		}

		return validFile;

	}

	/**
	 * 
	 * @return the ipmfile_Location
	 */

	public String getIpmfile_Location() {

		return ipmfile_Location;

	}

	/**
	 * 
	 * @return the project_Name
	 */

	public String getProject_Name() {

		return project_Name;

	}

	/**
	 * 
	 * @param project_Name
	 *            the project_Name to set
	 */

	public void setProject_Name(String project_Name) {

		this.project_Name = project_Name;

	}

	/**
	 * 
	 * @param ipmfile_Location
	 *            the ipmfile_Location to set
	 */

	public void setIpmfile_Location(String ipmfile_Location) {

		this.ipmfile_Location = ipmfile_Location;

	}

	/**
	 * @return the rts_Location
	 */
	public String getRts_Location() {
		return rts_Location;
	}

	/**
	 * @param rts_Location the rts_Location to set
	 */
	public void setRts_Location(String rts_Location) {
		this.rts_Location = rts_Location;
	}

	/**
	 * @return the sc_Location
	 */
	public String getSc_Location() {
		return sc_Location;
	}

	/**
	 * @param sc_Location the sc_Location to set
	 */
	public void setSc_Location(String sc_Location) {
		this.sc_Location = sc_Location;
	}

	/**
	 * @return the sourceCodeLocation
	 */
	public String getSourceCodeLocation() {
		return sourceCodeLocation;
	}

	/**
	 * @param sourceCodeLocation the sourceCodeLocation to set
	 */
	public void setSourceCodeLocation(String sourceCodeLocation) {
		this.sourceCodeLocation = sourceCodeLocation;
	}

}
